<?php

class FtpUploadHelper extends Helper {

    var $FtpModel;
    var $FtpUpload;

    public function __construct() {
        $this->FtpModel = $this->LoadModel('Public');
    }

    public function Set($FtpId = '0', $Type = 'image') {
        $this->_Conn = null;
        if (empty($FtpId)) {
            $FtpRow = $this->GetRand($Type);
        } else {
            $FtpRow = $this->GetId($FtpId);
        }
        if (!empty($FtpRow)) {
            $this->FtpUpload = new FtpUpload();
            $this->FtpUpload->_Id = $FtpRow['ftp_id'];
            $this->FtpUpload->_Enabled = $FtpRow['ftp_enabled'];
            $this->FtpUpload->_Url = $FtpRow['ftp_url'];
            $this->FtpUpload->_Config = array(
                'host' => $FtpRow['ftp_host'],
                'username' => $FtpRow['ftp_username'],
                'password' => Cookie::FauthCode($FtpRow['ftp_password'], 'DECODE', '1a2b3c4d5e'),
                'port' => $FtpRow['ftp_port'],
                'dir' => $FtpRow['ftp_dir'],
                'ssl' => empty($FtpRow['ftp_ssl']) ? '0' : '1'
            );
            $this->FtpUpload->_Dir = date('Ym') . '/' . date('d');
            $this->FtpUpload->FtpInit();
        }
    }

    public function Submit($File) {
        $UploadRow = array();
        if (!empty($this->FtpUpload->_Enabled)) {
            $UploadRow['path'] = $this->FtpUpload->FtpFile($File['url'], $File['name']);
            $UploadRow['ftp'] = $this->FtpUpload->_Id;
            $UploadRow['ftpurl'] = $this->FtpUpload->_Url;
            $this->FtpUpload->FtpClose();
        }
        return $UploadRow;
    }

    public function GetRand($Type = 'image') {
        Cache::Set('ftp', 'list', 30, 0);
        if (!$FtpAllCache = Cache::Get('all_' . $Type)) {
            $where = array();
            $where['`ftp_type` = ?'] = $Type;
            $where['`ftp_enabled` = ?'] = '1';
            $FtpAll = $this->FtpModel->getList('ftp', '*', $where, $limit = null, $group = null, $order = null, false);
            Cache::Save('all_' . $Type, $FtpAll);
        } else {
            $FtpAll = $FtpAllCache;
        }
        if (empty($FtpAll)) {
            return false;
        }
        mt_srand(doubleval(microtime() * 1000000));
        return $FtpAll[array_rand($FtpAll, 1)];
    }

    public function GetId($Id) {
        Cache::Set('ftp', 'detail', 30, 0);
        if (!$FtpRowCache = Cache::Get('id_' . $Id)) {
            $where = array();
            $where['`ftp_id` = ?'] = $Id;
            $FtpRow = $this->FtpModel->getOneRow('ftp', '*', $where);
            Cache::Save('id_' . $Id, $FtpRow);
        } else {
            $FtpRow = $FtpRowCache;
        }
        return $FtpRow;
    }

    public function ofileSort($dir, $data) {
        /* $dir = iconv('utf-8', 'gbk', $dir);
          $FtpRow = $this->GetRand();
          exit(var_export(!empty($FtpRow)));
          if (!empty($FtpRow)) {
          $this->FtpUpload = new FtpUpload();
          $this->FtpUpload->_Id = $FtpRow['ftp_id'];
          $this->FtpUpload->_Enabled = $FtpRow['ftp_enabled'];
          $this->FtpUpload->_Url = $FtpRow['ftp_url'];
          $this->FtpUpload->_Config = array(
          'host' => $FtpRow['ftp_host'],
          'username' => $FtpRow['ftp_username'],
          'password' => Cookie::FauthCode($FtpRow['ftp_password'], 'DECODE', '1a2b3c4d5e'),
          'port' => $FtpRow['ftp_port'],
          'dir' => '/attsort/',
          'ssl' => empty($FtpRow['ftp_ssl']) ? '0' : '1'
          );
          $this->FtpUpload->_Dir = $dir;
          $this->FtpUpload->FtpInit();
          $this->FtpUpload->delete_dir($dir); //删除旧文件夹
          $this->FtpUpload->FtpMkdir($dir);
          if(!empty($data)) foreach ($data as $k => $v){
          $result = $this->FtpUpload->copy_file($v['source'], $v['rename']);
          if(!$result['success']){
          return false;
          break;
          }
          }
          $this->FtpUpload->FtpClose();
          return true;
          } */
        $attdir = 'attsort/' . $dir;
        if (is_dir($attdir)) {
            //删除rename($attdir, 'attsort/.'.$dir);
            $this->deleteAll($attdir);
            //return true;
        }
        mkdir($attdir);
        if (!empty($data))
            foreach ($data as $k => $v) {
                if($v['type']=='卡单照片'){
                    $v['type']='合同照';
                    $v['rename']='_'.$v['rename'];
                }
                if($v['type']=='流水类' || $v['type']=='合同照') {
                    if (!is_dir($attdir . '/' . $v['type'])) {
                        mkdir($attdir . '/' . $v['type']);
                    }
                    copy(str_replace("/attach", "attach", $v['source']), $attdir . '/' . $v['type'] . '/' . $v['rename']);
                }else {
                    copy(str_replace("/attach", "attach", $v['source']), $attdir . '/' . $v['rename']);
                }
            }
        return true;
        //mkdir();
    }

    public function deleteAll($path) {
        $op = dir($path);
        while(false != ($item = $op->read())) {
            if($item == '.' || $item == '..') {
                continue;
            }
            if(is_dir($op->path.'/'.$item)) {
                $this->deleteAll($op->path.'/'.$item);
                @rmdir($op->path.'/'.$item);
            } else {
                unlink($op->path.'/'.$item);
            }

        }   
    }
}